﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CAD.BLL;
using CAD.DTO;

namespace WFASmartketing.PRESENTACION_GASTOS
{
    public partial class RegistroGastos : Form
    { private FrmPrincipal principal;
       
        public RegistroGastos(FrmPrincipal principal)
        {
            InitializeComponent();
            this.principal = principal;
            cbxcaja.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxtipogasto.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxresponsable.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void RegistroGastos_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tipoGastoDS.tipogasto' table. You can move, or remove it, as needed.
            this.tipogastoTableAdapter1.Fill(this.tipoGastoDS.tipogasto);
            // TODO: This line of code loads data into the 'tipodeGastoDS.tipogasto' table. You can move, or remove it, as needed.
            this.tipogastoTableAdapter.Fill(this.tipodeGastoDS.tipogasto);
            // TODO: This line of code loads data into the 'vendedorDS.vendedor_selectAll' table. You can move, or remove it, as needed.
            this.vendedor_selectAllTableAdapter.Fill(this.vendedorDS.vendedor_selectAll);
            // TODO: This line of code loads data into the 'cajaDS.caja_selectAll' table. You can move, or remove it, as needed.
            this.caja_selectAllTableAdapter.Fill(this.cajaDS.caja_selectAll);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void limpiar() {
            txtmonto.Text = "";
            txtobservacion.Text = ""; 
            dtcfecha.Value=DateTime.Now;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            limpiar();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double monto = 0;
            try
            {
                monto = Convert.ToDouble(txtmonto.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Debe ingresar un monto", "ERROR");
                return;
            }
            if (monto <= 0)
            {
                MessageBox.Show(this, "El monto debe ser mayor al 0", "ERROR");
                return;
            }
            DateTime fecha = dtcfecha.Value;
            int idcaja = 0;
            int responsable = 0;
            int tipogasto = 0;
            try
            {
                 idcaja = (int)cbxcaja.SelectedValue;
                 responsable = (int)cbxresponsable.SelectedValue;
                 tipogasto = (int)cbxtipogasto.SelectedValue;
            }
            catch (Exception)
            {
               MessageBox.Show(this, "No Existen Algunos Datos", "ADVERTENCIA");
                return;
            }
            string observacion = txtobservacion.Text;
            try
            {
                caja cuenta = cajaBLL.SelectByID(idcaja);
                double montoActual1 = cuenta.Monto - monto;
                cajaBLL.updatemonto(cuenta.Id, montoActual1);
                int nroRetiro = gastoBLL.insertar(tipogasto,responsable,principal.ObtenerLogueado().Id,DateTime.Now,idcaja,fecha,observacion,monto);
                MessageBox.Show(this, "Se registro el gasto correctamente, Nro gasto: " + nroRetiro, "GASTOS");
                limpiar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "No se registro el gasto", "ERROR");
            }
        }

        private void txtmonto_KeyPress(object sender, KeyPressEventArgs e)
        {
            UTIIIDADES.UTILIDADES.soloDecimanes(e,txtmonto);
        }
    }
}
